/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logica.jpaControllers;

import Entidades.Area;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entidades.Rolareamodulo;
import java.util.ArrayList;
import java.util.Collection;
import logica.jpaControllers.exceptions.IllegalOrphanException;
import logica.jpaControllers.exceptions.NonexistentEntityException;

/**
 *
 * @author Anali
 */
public class AreaJpaController {

    public AreaJpaController() {
        emf = Persistence.createEntityManagerFactory("GestorPesajeJpaPU2");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Area area) {
        if (area.getRolareamoduloCollection() == null) {
            area.setRolareamoduloCollection(new ArrayList<Rolareamodulo>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Rolareamodulo> attachedRolareamoduloCollection = new ArrayList<Rolareamodulo>();
            for (Rolareamodulo rolareamoduloCollectionRolareamoduloToAttach : area.getRolareamoduloCollection()) {
                rolareamoduloCollectionRolareamoduloToAttach = em.getReference(rolareamoduloCollectionRolareamoduloToAttach.getClass(), rolareamoduloCollectionRolareamoduloToAttach.getIdRolAreaModulo());
                attachedRolareamoduloCollection.add(rolareamoduloCollectionRolareamoduloToAttach);
            }
            area.setRolareamoduloCollection(attachedRolareamoduloCollection);
            em.persist(area);
            for (Rolareamodulo rolareamoduloCollectionRolareamodulo : area.getRolareamoduloCollection()) {
                Area oldAreaOfRolareamoduloCollectionRolareamodulo = rolareamoduloCollectionRolareamodulo.getArea();
                rolareamoduloCollectionRolareamodulo.setArea(area);
                rolareamoduloCollectionRolareamodulo = em.merge(rolareamoduloCollectionRolareamodulo);
                if (oldAreaOfRolareamoduloCollectionRolareamodulo != null) {
                    oldAreaOfRolareamoduloCollectionRolareamodulo.getRolareamoduloCollection().remove(rolareamoduloCollectionRolareamodulo);
                    oldAreaOfRolareamoduloCollectionRolareamodulo = em.merge(oldAreaOfRolareamoduloCollectionRolareamodulo);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Area area) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Area persistentArea = em.find(Area.class, area.getIdArea());
            Collection<Rolareamodulo> rolareamoduloCollectionOld = persistentArea.getRolareamoduloCollection();
            Collection<Rolareamodulo> rolareamoduloCollectionNew = area.getRolareamoduloCollection();
            List<String> illegalOrphanMessages = null;
            for (Rolareamodulo rolareamoduloCollectionOldRolareamodulo : rolareamoduloCollectionOld) {
                if (!rolareamoduloCollectionNew.contains(rolareamoduloCollectionOldRolareamodulo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Rolareamodulo " + rolareamoduloCollectionOldRolareamodulo + " since its area field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Rolareamodulo> attachedRolareamoduloCollectionNew = new ArrayList<Rolareamodulo>();
            for (Rolareamodulo rolareamoduloCollectionNewRolareamoduloToAttach : rolareamoduloCollectionNew) {
                rolareamoduloCollectionNewRolareamoduloToAttach = em.getReference(rolareamoduloCollectionNewRolareamoduloToAttach.getClass(), rolareamoduloCollectionNewRolareamoduloToAttach.getIdRolAreaModulo());
                attachedRolareamoduloCollectionNew.add(rolareamoduloCollectionNewRolareamoduloToAttach);
            }
            rolareamoduloCollectionNew = attachedRolareamoduloCollectionNew;
            area.setRolareamoduloCollection(rolareamoduloCollectionNew);
            area = em.merge(area);
            for (Rolareamodulo rolareamoduloCollectionNewRolareamodulo : rolareamoduloCollectionNew) {
                if (!rolareamoduloCollectionOld.contains(rolareamoduloCollectionNewRolareamodulo)) {
                    Area oldAreaOfRolareamoduloCollectionNewRolareamodulo = rolareamoduloCollectionNewRolareamodulo.getArea();
                    rolareamoduloCollectionNewRolareamodulo.setArea(area);
                    rolareamoduloCollectionNewRolareamodulo = em.merge(rolareamoduloCollectionNewRolareamodulo);
                    if (oldAreaOfRolareamoduloCollectionNewRolareamodulo != null && !oldAreaOfRolareamoduloCollectionNewRolareamodulo.equals(area)) {
                        oldAreaOfRolareamoduloCollectionNewRolareamodulo.getRolareamoduloCollection().remove(rolareamoduloCollectionNewRolareamodulo);
                        oldAreaOfRolareamoduloCollectionNewRolareamodulo = em.merge(oldAreaOfRolareamoduloCollectionNewRolareamodulo);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = area.getIdArea();
                if (findArea(id) == null) {
                    throw new NonexistentEntityException("The area with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Area area;
            try {
                area = em.getReference(Area.class, id);
                area.getIdArea();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The area with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Rolareamodulo> rolareamoduloCollectionOrphanCheck = area.getRolareamoduloCollection();
            for (Rolareamodulo rolareamoduloCollectionOrphanCheckRolareamodulo : rolareamoduloCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Area (" + area + ") cannot be destroyed since the Rolareamodulo " + rolareamoduloCollectionOrphanCheckRolareamodulo + " in its rolareamoduloCollection field has a non-nullable area field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(area);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Area> findAreaEntities() {
        return findAreaEntities(true, -1, -1);
    }

    public List<Area> findAreaEntities(int maxResults, int firstResult) {
        return findAreaEntities(false, maxResults, firstResult);
    }

    private List<Area> findAreaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Area as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Area findArea(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Area.class, id);
        } finally {
            em.close();
        }
    }

    public int getAreaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Area as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
      public Area findArea_x_nombre(String nom) {
         EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select u from Area u where u.nombreArea = " + "'" + nom +"'");
            return (Area) q.getSingleResult();
        } finally {
            em.close();
        }
    }

}
